package com.example.guiguiapplicatio.utils;

import android.content.Context;
import android.view.View;
import android.widget.Toast;

import androidx.databinding.InverseMethod;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class IsNumericUtils {
    private static List<String> strings=new ArrayList<>();
    private static Context context;
    public static IsNumericUtils getInstance() {
        return new IsNumericUtils();
    }
    public IsNumericUtils addString(String s){
        if (s!=null){
            strings.add(s);
        }
        return this;
    }
    public  IsNumericUtils addContext(Context context){
        strings.clear();
        this.context=context;
        return this;
    }

    public static boolean isNumeric(String str){

        Pattern pattern = Pattern.compile("[0-9]*\\.?[0-9]+");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;


    }

    public  boolean isAllNumeric(){
        boolean b=true;
        for (int i = 0; i < strings.size(); i++) {
            if (strings.get(i).length()>0){
                if (isNumeric(strings.get(i))!=true){
                    if (context!=null){
                        Toast.makeText(context, "存在格式错误，注意数字以及不要存留空格！", Toast.LENGTH_LONG).show();
                        Toast.makeText(context, "错误源： "+strings.get(i), Toast.LENGTH_LONG).show();
                    }
                    b=false;
                    break;
                }
            }
        }
        return b;

    }

}
